//
//  SearchCellWithLine.m
//  danpin
//
//  Created by chuangjia on 25/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "SearchCellWithLine.h"

@interface SearchCellWithLine()<UITextFieldDelegate>

@property(nonatomic,strong)UIView* vSearchTxt;
@property(nonatomic,strong)UIImageView* imgSearch;
@property(nonatomic,strong)UITextField* txtKey;
@property(nonatomic,strong)UIButton* keyBtn;
@property(nonatomic,strong)UIImageView* lineView;
@end
@implementation SearchCellWithLine

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor whiteColor];
        
        _vSearchTxt = [[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*2, 0,
                                                               DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4)];
        _vSearchTxt.backgroundColor = [UIColor whiteColor];//UIColorFromRGB(0xf9f9f9);
        _vSearchTxt.layer.cornerRadius = 6;
        _vSearchTxt.layer.borderWidth = 0.5;
        _vSearchTxt.layer.borderColor = UIColorFromRGB(0x999999).CGColor;
        [self.contentView addSubview:_vSearchTxt];
        
        _imgSearch = [[UIImageView alloc] initWithFrame:CGRectMake((PHOTO_FRAME_WIDTH*4-14)/2, (PHOTO_FRAME_WIDTH*4-14)/2, 14, 14)];
        _imgSearch.image = [UIImage imageNamed:@"search_icon"];
        [_vSearchTxt addSubview:_imgSearch];
        
        _txtKey = [[UITextField alloc] initWithFrame:CGRectMake(_imgSearch.frame.origin.x+_imgSearch.frame.size.width+6, 0, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*4)];
        _txtKey.placeholder = @"搜索单品...";
        _txtKey.font = FONT(14);
        _txtKey.clearButtonMode = UITextFieldViewModeWhileEditing;
        _txtKey.returnKeyType = UIReturnKeySearch;
        _txtKey.delegate = self;
        [_vSearchTxt addSubview:_txtKey];
        _keyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _keyBtn.frame =CGRectMake(0, 0,DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH*4);
        [_keyBtn addTarget:self action:@selector(searchClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_vSearchTxt addSubview:_keyBtn];
//        
//        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
//        _lineView.image =[UIImage imageNamed:@"book_line.png"];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.vSearchTxt=nil;
    self.imgSearch=nil;
    self.txtKey=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
//    self.lineView.frame=CGRectMake(0, self.bounds.size.height-10, DEVICE_WIDTH, 10);
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    self.data =model;
    self.indexPath =indexPath;
    if(model && [model isKindOfClass:[NSString class]]&&[model isEqualToString:@"index"])
    {
    }else{
        _keyBtn.hidden = YES;
    }
    
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    
    return PHOTO_FRAME_WIDTH*5;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (self.actionBlock) {
        self.actionBlock(BEventType_search,nil,textField.text,nil,self.indexPath);
    }
    return YES;
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)searchClicked:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_search,sender,self.data,nil,self.indexPath);
    }
}
@end
